/**
 * 1. 爬图片保存在 /output/YYYY-MM-DD-date/bg.png
 * 2. 处理文案图,文案+文案背景(message_image_base.png) => 文案图 保存在 /output/YYYY-MM-DD-date/message_image_bg.png
 * 3. 基于 FFCreator 生成短视频
 */

const fs = require('fs-extra')
const path = require('path')
const bg_spider = require('./src/bg_spider')
const message_bg_creator = require('./src/message_bg_creator')
const video_creator = require('./src/video_creator')

// 两个文案图配置
const messages = [
  {
    message: '这是中文文案\n',
    name: 'message_image_bg_1.png'
  },
  {
    message: 'This is English copywriter\n',
    name: 'message_image_bg_2.png'
  }
]
// 背景壁纸连接
const BG_LINK = 'https://www.3gbizhi.com/wallQFJ/4590.html'
const OUTPUT_DIR = path.resolve(__dirname, `./output/${new Date().getTime()}`)
const CACHE_DIR = path.resolve(__dirname, `./cache`)
const MESSAGE_BASE_IMAGE_DIR = path.resolve(__dirname, './assets/image/message_image_base.png')

async function main() {
  // 新建文件输入目录
  fs.mkdirSync(OUTPUT_DIR)
  // 爬取视频背景图
  const bg = await bg_spider(BG_LINK, OUTPUT_DIR)
  // 输出文案背景图
  let imgs = []
  for (const item of messages) {
    let img = await message_bg_creator(
      item.message,
      MESSAGE_BASE_IMAGE_DIR,
      path.resolve(__dirname, `${OUTPUT_DIR}/${item.name}`)
    )
    imgs.push(img)
  }

  // 生成短视频
  try {
    await video_creator({
      bg,
      img1: imgs[0],
      img2: imgs[1],
      outputDir: OUTPUT_DIR,
      cacheDir: CACHE_DIR
    })
  } catch (error) {
    console.log(
      '%c 🍤 video_creator error: ',
      'font-size:20px;background-color: #F5CE50;color:#fff;',
      error
    )
  }
}

main()
